<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>期刊论文管理系统 - 注册</title>
    <!-- Bootstrap CSS和JS -->
    <link href="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script>
    <style>
        body {
            background: linear-gradient(173deg, #ecf4ff -.79%, #d3e1ff 94.5%);
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .register-container {
            background: #fff;
            border: 1px solid rgba(28, 31, 35, .08);
            border-radius: 12px;
            box-shadow: 0 4px 14px 0 rgba(0, 0, 0, .1), 0 0 1px 0 rgba(0, 0, 0, .3);
            padding: 2rem;
            max-width: 400px;
            width: 100%;
        }
        .register-container img {
            display: block;
            margin: 0 auto 1rem;
            width: 150px;
        }
        .register-container h2 {
            color: #1c1d23;
            font-size: 22px;
            font-weight: 600;
            text-align: center;
            margin-bottom: 0rem;
        }
        .form-group label {
            font-size: 14px;
        }
        .btn-register {
            width: 100%;
            font-size: 16px;
        }
        .login-link {
            text-align: center;
            margin-top: 1.5rem;
        }
        .error-message {
            color: red;
            text-align: center;
            margin-top: 1rem;
        }
    </style>
</head>
<body>
<div class="register-container">
    <img src="img/logo.png" alt="Logo">
    <h2>注册账号</h2>
    <form id="registerForm" class="needs-validation" novalidate>
        <div class="mb-3">
            <label for="uname" class="form-label">用户名:</label>
            <input type="text" class="form-control" id="uname" placeholder="请输入用户名" name="uname" required>
            <div class="invalid-feedback">请输入用户名！</div>
        </div>
        <div class="mb-3">
            <label for="pwd" class="form-label">密码:</label>
            <input type="password" class="form-control" id="pwd" placeholder="请输入密码" name="pwd" required>
            <div class="invalid-feedback">请输入密码！</div>
        </div>
        <div class="mb-3">
            <label for="confirmPwd" class="form-label">确认密码:</label>
            <input type="password" class="form-control" id="confirmPwd" placeholder="请确认密码" name="confirmPwd" required>
            <div class="invalid-feedback">请再次输入密码以确认！</div>
        </div>
        <button type="submit" class="btn btn-primary btn-register">注册</button>
    </form>
    <div class="login-link">
        <a href="login.html">已有账号？返回登录页面</a>
    </div>
    <div id="errorMessage" class="error-message"></div>
</div>

<script>
    // 表单验证逻辑
    (function() {
        'use strict';
        window.addEventListener('load', function() {
            var forms = document.getElementsByClassName('needs-validation');
            Array.prototype.filter.call(forms, function(form) {
                form.addEventListener('submit', function(event) {
                    event.preventDefault();
                    event.stopPropagation();

                    if (form.checkValidity()) {
                        // 密码确认检查
                        const pwd = document.getElementById('pwd').value;
                        const confirmPwd = document.getElementById('confirmPwd').value;
                        if (pwd !== confirmPwd) {
                            document.getElementById('errorMessage').textContent = '两次输入的密码不一致！';
                            return;
                        }
                        const username = document.getElementById('uname').value;
                        const password = pwd;

                        fetch('http://127.0.0.1:8080/api/user/register', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify({ username, password })
                        })
                            .then(response => response.json())
                            .then(result => {
                                if (result.success) {
                                    alert('注册成功！');
                                    // 注册成功后重定向到登录页面
                                    window.location.href = 'login.html';
                                } else {
                                    document.getElementById('errorMessage').textContent = result.message || '注册失败，请尝试不同的用户名。';
                                }
                            })
                            .catch(error => {
                                console.error('请求失败:', error);
                                document.getElementById('errorMessage').textContent = '网络错误，请稍后再试。';
                            });
                    }
                    form.classList.add('was-validated');
                }, false);
            });
        }, false);
    })();
</script>
</body>
</html>